//
//  detailViewController.m
//  testassembly
//
//  Created by liangang on 2018/3/28.
//  Copyright © 2018年 liangang. All rights reserved.
//

#import "detailViewController.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"
#import "AppDelegate.h"
@interface detailViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    NJKWebViewProgressView *_webViewProgressView;
    NJKWebViewProgress *_webViewProgress;
    NSURLRequest *request;
//    UIView *tempView;
}
@property (nonatomic,strong)UIWebView *webView;
@end

@implementation detailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
//    self.automaticallyAdjustsScrollViewInsets   = NO;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#2fb0f4"];
    self.webView.scrollView.frame =self.webView.frame;
    self.webView .scrollView.backgroundColor = [UIColor whiteColor];
    _webView.backgroundColor =[UIColor colorWithHexString:@"#2fb0f4"];
    if (KIsiPhoneX) {
         _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, kScreenHeight-44)];
//        _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, 2)];
    }
    else
    {
//        _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 2)];
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-20)];
    }
//    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-0)];
    _webView.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.url]]];
    _webView.scalesPageToFit = YES;
    [ self.webView loadRequest:req];
    [self.view addSubview:self.webView];
    
//    _webViewProgress = [[NJKWebViewProgress alloc] init];
//    _webView.delegate = _webViewProgress;
//    _webViewProgress.webViewProxyDelegate = self;
//    _webViewProgress.progressDelegate = self;
    
//    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 2)];
//    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
//    [self.view addSubview:_webViewProgressView];
//    [_webViewProgressView setProgress:0 animated:YES];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    back.tintColor = [UIColor blackColor];
    UIBarButtonItem *close = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"close"] style:UIBarButtonItemStyleDone target:self action:@selector(close)];
    close.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItems = @[back,close];
     _webView.scrollView.bounces = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWeb) name:@"RELOADWEBVIEW" object:nil];
    if (@available(iOS 11.0, *)) {
        _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    // Do any additional setup after loading the view.
}
-(void)reloadWeb
{
    [_webView reload];
}
-(void)back
{
    if ([_webView canGoBack]) {
        [_webView goBack ];
    }
    else
        [self.navigationController popViewControllerAnimated:YES];
}
-(void)close
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
   
//
    
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    
//    if (!tempView ) {
//        tempView = [[UIView alloc]init];
//    }
//    if (!KIsiPhoneX) {
//        [[UIApplication sharedApplication].keyWindow addSubview:tempView];
//        tempView.backgroundColor = [UIColor whiteColor    ];
//        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.equalTo(tempView.superview);
//            make.bottom.equalTo(tempView.superview).offset(-49);
//            make.height.mas_equalTo(20);
//        }];
//    }
   
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"--------------%@",error);
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//    [tempView removeFromSuperview];
    if ([webView.request.URL.absoluteString containsString:@"https://www.lialoan.com"]||[webView.request.URL.absoluteString containsString:@"http://suo.im"]) {
        
    }
    else
    {
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]]];
        MBProgressHUD *hud = [[MBProgressHUD alloc]initWithWindow:[UIApplication sharedApplication].keyWindow];
        [[UIApplication sharedApplication].keyWindow addSubview:hud];
        
        
        //    MBProgressHUD *hud = [MBProgressHUD alloc]
        hud.mode = MBProgressHUDModeText;
        [hud setRemoveFromSuperViewOnHide:YES];
        hud.detailsLabelText = @"扫码失败，请重试";
        [hud show:YES];
        [hud hide:YES afterDelay:2.0f];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
    
    NSLog(@"%@",request.URL.absoluteString);
    //    NSString *currentURL= webView.request.URL.absoluteString;
    NSString *scheme = request.URL.scheme;
    if ([scheme isEqualToString:@"driver"]) {
        NSString *host =  request.URL.host;
        if ([host isEqualToString:@"goToOpenScan"]) {
//            [[webMethod  sharedInstance] openScane];
            
        }
        else if ([host isEqualToString:@"goToFaceAuth"])
        {
            [[webMethod sharedInstance] FaceAuth:self];
        }
        
        else if ([host isEqualToString:@"goToContacts"])
        {
            [[webMethod sharedInstance] Contacts    ];
        }
        
        else if ([host isEqualToString:@"goToVideo"])
        {
            [[webMethod sharedInstance] Video];
        }
        else if ([host isEqualToString:@"goToNav"])
        {
            NSString *str = [request.URL lastPathComponent];
            NSArray *array = [str componentsSeparatedByString:@","];
            [[webMethod sharedInstance] goToNavLat:array[0] WithLon:array[1]];
        }
        else if ([host isEqualToString:@"goToAlipay"])
        {
//            NSString *str = [request.URL lastPathComponent];
//            NSArray *array = [str componentsSeparatedByString:@","];
//            [[webMethod sharedInstance] goToNavLat:array[0] WithLon:array[1]];
            [[webMethod   sharedInstance] alipayWithOrderNumber:request.URL.lastPathComponent];
            
        }
        else if ([host isEqualToString:@"goToMyCenter"])
        {
//            self.hidesBottomBarWhenPushed = NO;
            [self.navigationController popViewControllerAnimated:NO];
            
            AppDelegate *delegat = [UIApplication sharedApplication].delegate;
            [delegat.tabBar setSelectedIndex:3];
        }
        else if ([host isEqualToString:@"goToBack"])
        {
            if ([webView canGoBack]) {
                [webView goBack];
            }
            else
            [self.navigationController popViewControllerAnimated:YES];
          
        }
        
        else if ([host isEqualToString:@"goToClose"])
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else if ([host isEqualToString:@"goToShareWx"])
        {
            NSString *str = request.URL.path;
            
            NSArray *array = [str componentsSeparatedByString:@"https://"];
            if (array.count==2) {
                NSString *title = [array[0] stringByReplacingOccurrencesOfString:@"/" withString:@""];
                NSString *url = [NSString stringWithFormat:@"https://%@",array[1]];
                NSDictionary *dic =@{@"title":title,@"url":url};
                [[webMethod sharedInstance] shareAction:dic WithType:WX];
            }
            
        }
        else if ([host isEqualToString:@"goToShareWxPy"])
        {
            NSString *str = request.URL.path;
            
            NSArray *array = [str componentsSeparatedByString:@"https://"];
            if (array.count==2) {
                NSString *title = [array[0] stringByReplacingOccurrencesOfString:@"/" withString:@""];
                NSString *url = [NSString stringWithFormat:@"https://%@",array[1]];
                NSDictionary *dic =@{@"title":title,@"url":url};
                [[webMethod sharedInstance] shareAction:dic WithType:WXPY];
            }
            
        }
        else if ([host isEqualToString:@"goToShareQQ"])
        {
            NSString *str = request.URL.path;
            
            NSArray *array = [str componentsSeparatedByString:@"https://"];
            if (array.count==2) {
                NSString *title = [array[0] stringByReplacingOccurrencesOfString:@"/" withString:@""];
                NSString *url = [NSString stringWithFormat:@"https://%@",array[1]];
                NSDictionary *dic =@{@"title":title,@"url":url};
                [[webMethod sharedInstance] shareAction:dic WithType:QQ];
            }
            
        }
        else if ([host isEqualToString:@"goToWxPay"])
        {
            [[webMethod sharedInstance] WxPayActionWithOrder:request.URL.lastPathComponent];
        }
        
        return NO;
    }
    else if ([scheme isEqualToString:@"target"])
    {
        NSString * host = request.URL.host;
        return NO;
        
    }
    //    NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    //    self.title = theTitle   ;
    else
        return YES;
}
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_webViewProgressView setProgress:progress animated:YES];
    self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
